package edu.yngsxy.demo.lanjieqi;

import edu.yngsxy.demo.model.TUser;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用户权限拦截器规则
 */
@Component
public class Lanjieqi implements HandlerInterceptor {
    /*
    /**
     * 用户请求URL之前进行拦截处理
     * @return Boolean true 不在拦截，继续执行URL的执行，flase拦截，后续URL不再被请求到
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获得登录的用户，并根据登录用户不同的类型来决定不同的URL是否能被通过
        HttpSession session = request.getSession();//获得session
        TUser loginuser = (TUser) session.getAttribute("loginuser");

        //获取请求的URI
        String requestURI = request.getRequestURI();
        if (loginuser == null) {
            if (requestURI.equals("/product/modProduct") || requestURI.equals("/product/deleteProduct") ||
                    requestURI.equals("/product/productAll") || requestURI.equals("/product/addProduct")){
                response.setStatus(401);
                return false;
            } else {
                return true;
            }
        }else{
            return true;
        }

    }




    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                                Exception ex) throws Exception {

    }

}

